﻿using DataWorks_Tools.BasicMethods;
using DataWorks_Tools.MappModals.CalInputModals;
using DataWorks_Tools.MappModals.CalSaveModals;

using Yitter.IdGenerator;
using static DataWorks_Tools.BasicMethods.ReadfromCSV;

namespace DataWorks_Tools.HalfHourCalSave.Common.DataImport
{
    public static class Tools_GPSImport
    {
        public static async Task<List<calsave_gps_split>> GetHalfHourGPSAsync(CSVImport csvimport, HalfHourCalInputModel props, int reducetime)
        {
            List<calsave_gps_split> list = new List<calsave_gps_split>();
            if (props.importgps == 1)
            {
                if (props.chantitle_spd != null)
                {
                    var latlist = await csvimport.GetSpecifiedColumnDataAsync("lat"); 
                    var lonlist = await csvimport.GetSpecifiedColumnDataAsync("lon");
                    var altilist = await csvimport.GetSpecifiedColumnDataAsync("alti");
                    var spdlist = await csvimport.GetSpecifiedColumnDataAsync(props.chantitle_spd.ReName());
                    if(altilist?.Count>0)
                    {
                        if (latlist?.Count == lonlist?.Count && latlist?.Count == spdlist?.Count && latlist?.Count == altilist?.Count)
                        {
                            var t1 = latlist?.Where((item, index) => index % reducetime == 0).ToList();
                            var t2 = lonlist?.Where((item, index) => index % reducetime == 0).ToList();
                            var t3 = spdlist?.Where((item, index) => index % reducetime == 0).ToList();
                            var t4 = altilist?.Where((item, index) => index % reducetime == 0).ToList();
                            if (t1 != null && t2 != null && t3 != null && t4 != null)
                            {
                                for (int i = 0; i < t1?.Count; i++)
                                {
                                    calsave_gps_split gps = new calsave_gps_split();
                                    gps.filename = csvimport.name;
                                    gps.vehicle = props.vehicle;
                                    gps.project = props.project;
                                    gps.datadate = Convert.ToDateTime(csvimport.datetimesecond);
                                    gps.subdirectory = csvimport.subdirectory;
                                    gps.createTime = Convert.ToDateTime(csvimport.datetime);
                                    gps.key = YitIdHelper.NextId();
                                    gps.lat = t1[i];
                                    gps.lon = t2[i];
                                    gps.alt = t4[i];
                                    gps.spd = t3[i];
                                    list.Add(gps);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (latlist?.Count == lonlist?.Count && latlist?.Count == spdlist?.Count)
                        {
                            var t1 = latlist?.Where((item, index) => index % reducetime == 0).ToList();
                            var t2 = lonlist?.Where((item, index) => index % reducetime == 0).ToList();
                            var t3 = spdlist?.Where((item, index) => index % reducetime == 0).ToList();
                            if (t1 != null && t2 != null && t3 != null)
                            {
                                for (int i = 0; i < t1?.Count; i++)
                                {
                                    calsave_gps_split gps = new calsave_gps_split();
                                    gps.filename = csvimport.name;
                                    gps.vehicle = props.vehicle;
                                    gps.project = props.project;
                                    gps.datadate = Convert.ToDateTime(csvimport.datetimesecond);
                                    gps.subdirectory = csvimport.subdirectory;
                                    gps.createTime = Convert.ToDateTime(csvimport.datetime);
                                    gps.key = YitIdHelper.NextId();
                                    gps.lat = t1[i];
                                    gps.lon = t2[i];
                                    gps.alt = 0;
                                    gps.spd = t3[i];
                                    list.Add(gps);
                                }
                            }
                        }
                    }
                    
                }
            }
            return list;

        }

    }
}
